﻿using INTEX.DataBase.Tools;
using INTEX.SCHEDULER;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Data;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public class VSXNAB02:ViewModelBase
    {
             
        public ITXSSPA Scheduler=new ITXSSPA();

        private bool _IsShowGantt;
        public bool IsShowGantt
        {
            get { return _IsShowGantt; }
            set
            {
                _IsShowGantt = value;
                OnPropertyChanged("IsShowGantt", value);
            }
        }
        public CollectionView vwVKPOS
        {
            get
            {
                return (CollectionView)CollectionViewSource.GetDefaultView(SetData.Tables["VKPOS"]);
            }
        }
        private DataRowView _dwVKPOS = null;
        public DataRowView dwVKPOS
        {
            get
            {
                return _dwVKPOS;
            }
            set
            {
                _dwVKPOS = value;
                OnPropertyChanged("dwVKPOS", value);
                if (value == null)
                    return;
                using (new LongOperation(new Controls.TBTN(), "读取符合条件的记录"))
                {
                    IsGlobalStart = true;
                    DataRow _dwS = VICC_STOCK.SetProd.Tables["ICC_STOCK"].NewRow();
                    _dwS["ART"] = _dwVKPOS["GAART"].ToString();
                    _dwS["VAR"] = _dwVKPOS["GAVAR"].ToString();
                    _dwS["FARBE"] = _dwVKPOS["GAFARBE"].ToString();
                    _dwS["LO"] = "01";
                    _dwS["LB"] = "CP";
                    _dwS["MDATE"] = date;
                    _dwS["SPPART"] = _dwVKPOS["SPINLOT"].ToString();
                    dwSTOCK = _dwS;
                }
            }
        }
        private bool _IsSXNAB02; public bool IsSXNAB02 { get { return _IsSXNAB02; } set { _IsSXNAB02 = value; OnPropertyChanged("IsSXNAB02", value); } }
        private string _KUNDNR; public string KUNDNR { get { return _KUNDNR; } set { _KUNDNR = value; OnPropertyChanged("KUNDNR", value); } }
        private string _YARNTYP; public string YARNTYP { get { return _YARNTYP; } set { _YARNTYP = value; OnPropertyChanged("YARNTYP", value); } }
        private string _SPINLOT; public string SPINLOT { get { return _SPINLOT; } set { _SPINLOT = value; OnPropertyChanged("SPINLOT", value); } }
        private string _YNE; public string YNE { get { return _YNE; } set { _YNE = value; OnPropertyChanged("YNE", value); } }
        private string _SACHBE; public string SACHBE { get { return _SACHBE; } set { _SACHBE = value; OnPropertyChanged("SACHBE", value); } }
        private string _BESTNR; public string BESTNR { get { return _BESTNR; } set { _BESTNR = value; OnPropertyChanged("BESTNR", value); } }
        private string _KDKOMM; public string KDKOMM { get { return _KDKOMM; } set { _KDKOMM = value; OnPropertyChanged("KDKOMM", value); } }
        private string _LIEFTERM; public string LIEFTERM { get { return _LIEFTERM; } set { _LIEFTERM = value; OnPropertyChanged("LIEFTERM", value); } }
        private DataRow _dwSTOCK; public DataRow dwSTOCK { get { return _dwSTOCK; } set { _dwSTOCK = value; OnPropertyChanged("dwSTOCK", value); } }
        private string _KSTKRS; public string KSTKRS { get { return _KSTKRS; } set { _KSTKRS = value; OnPropertyChanged("KSTKRS", value); } }
        private string _KST; public string KST { get { return _KST; } set { _KST = value; OnPropertyChanged("KST", value); } }
        private string _AGANG; public string AGANG { get { return _AGANG; } set { _AGANG = value; OnPropertyChanged("AGANG", value); } }
        private string _AMTYP; public string AMTYP { get { return _AMTYP; } set { _AMTYP = value; OnPropertyChanged("AMTYP", value); } }
        private string _JOBTYPE;
        public string JOBTYPE
        {
            get
            {
                return _JOBTYPE;
            }
            set
            {
                _JOBTYPE = value;
                OnPropertyChanged("JOBTYPE");
            }
        }

        VKDSTAMM VKDSTAMM;
        VVKPOS VVKPOS = new VVKPOS();
        VABPOS VABPOS ;
        VAMSTAMM VAMSTAMM = new VAMSTAMM();
        VVVTLIEF VVVTLIEF;
        VVRPOS VVRPOS = new VVRPOS();
        VICC_MKRF VICC_MKRF = new VICC_MKRF();
        VICC_PVKPOS VICC_PVKPOS = new VICC_PVKPOS();
        VVAFARBEN VVAFARBEN;
        VICC_ABPOS_MCO VICC_ABPOS_MCO = new VICC_ABPOS_MCO();
        VICC_STOCK VICC_STOCK = new VICC_STOCK();
        VVKKOPF VVKKOPF; 
        VWDECK VWDECK;
        VWGBST VWGBST;
        public VSXNAB02()
            : base()
        {
            IsCostMode = true;
            IsSXNAB02 = true;

            SetEnv = new DataSet();
            SetData = new DataSet();
            SetProd = new DataSet();
             QueryCommand = new DelegateCommand(x => Query());
            _dwSTOCK = VICC_STOCK.SetProd.Tables["ICC_STOCK"].NewRow();
            Scheduler.MODE = "P";
            KSTKRS = "99";
            KST = "9999";
            _KUNDNR = "";
            _YNE = "/";
            _YARNTYP = "";
            _BESTNR = "";
            _KDKOMM = "";
            AGANG = Helper.Left("999".PadRight(15), 15);
            AMTYP = Helper.Left("99999999".PadRight(8), 8);
            _LIEFTERM = date;
            if (app.User.Trim() == "ITXV")
            {
                KRS = "15";
                LFD = "33";
                SACHBE = "";
                _LIEFTERM = "20150101";
            }
            else
            {
                SACHBE = app.User;
            }
        }

        public void Query()
        {
            DataSet SResult = new DataSet();
            DataRow dwData = SetEnv.Tables["VKPOS"].NewRow();
            dwData["VKPMANDANT"] = app.Mandant;
            dwData["VKPAUFKRS"] = KRS;
            dwData["VKPAUFLFD"] = LFD;
            if (LFD == "000000")
            {
                dwData["VKPAUFLFD"] = "";
            }
            dwData["VKPKUNDNR"] = _KUNDNR;
            dwData["VKPKDBEZ"] = _YARNTYP;
            dwData["VKPAENDST"] = _YNE;
            dwData["VKPART"] = _SPINLOT;
            dwData["VKPSACHBE"] = _SACHBE;
            dwData["VKPFILLER2"] = _BESTNR;
            dwData["VKPKDKOMM"] = _KDKOMM;
            if (_LIEFTERM.Trim() == "")
                _LIEFTERM = "20100101";
            dwData["VKPLIEFTERM"] = _LIEFTERM;

            SResult = VVKPOS.QueryVKPOS(dwData, 11);
            if (SetProd.Tables.Contains("VKPOS"))
            {
                SetProd.Tables["VKPOS"].Clear();
            }
            SetProd.Merge(SResult);
        }
        public override void LoadEnv()
        {
            base.LoadEnv();
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataRow dwData;

            dwData = VAMSTAMM.SetEnv.Tables["AMSTAMM"].NewRow();
            dwData["AMMANDANT"] = "00";
            dwData["AMKSTKRS"] = KSTKRS;
            dwData["AMKST"] = KST;
            dwData["AMTYP"] = AMTYP;
            SResult = VAMSTAMM.QueryAMSTAMM(dwData, 1);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                row["AMBEZ"] = Helpers.Helper.GetGB2312Value(row["AMBEZ"].ToString().Trim());
                row["AMAENDZL"] = 1;//排产最后日期
                row["AMAENDDAT"] = 0;//排产最后日期
                row["AMNEUDAT"] = "0000";//排产最后时间
            }
            SetEnv.Merge(SResult);
            INTEXDBC.DeleteICC_STDMACHINE(null, 1);
            INTEXDBC.InsertICC_STDMACHINE(null, 1);
            SResult = INTEXDBC.QueryICC_STDMACHINE(dwData, 1);
            SetEnv.Merge(SResult);
            SResult = INTEXDBC.QueryICC_SRULES(null, 9999);
            SetEnv.Merge(SResult);
            SResult = INTEXDBC.QueryICC_PLANSETUP(null, 9999);
            SetEnv.Merge(SResult);
        }
 
   
      
        public void GetOrderNetWork(string _krs, string _lfd, string _pos)
        {
         
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataSet STemp = new DataSet();
            DataRow dwData = SetEnv.Tables["ICC_QUERYTB"].NewRow();
            string EXART = _krs + "-" + Helpers.Helper.Right(_lfd, 4) + "-" + _pos;
            SReturn = JLTON(_krs, _lfd, _pos, EXART);
            SetData.Clear();
            SetData.Merge(SReturn);
        }
        public  void BuildData(DataRow dwInput)
        { 
            try
            {
                GetOrderNetWork(KRS, LFD, POS);
                SetEnv.Tables["ICC_MKRF"].Clear();
                if (SetData.Tables.Contains("ICC_MKRF"))
                {
                    SetEnv.Tables["ICC_MKRF"].Merge(SetData.Tables["ICC_MKRF"]);
                }
                if (SetData.Tables.Contains("ABFOLGE"))
                {
                    if (SetEnv.Tables.Contains("ABFOLGE"))
                    {
                        SetEnv.Tables["ABFOLGE"].Clear();
                    }
                    SetEnv.Merge(SetData.Tables["ABFOLGE"]);
                }
                if (SetData.Tables.Contains("KSLEIST"))
                {
                    if (SetEnv.Tables.Contains("KSLEIST"))
                    {
                        SetEnv.Tables["KSLEIST"].Clear();
                    }
                    SetEnv.Merge(SetData.Tables["KSLEIST"]);
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
     
        private DataSet JLTON(string _krs, string _lfd, string _pos, string _EXART)
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataSet STemp = new DataSet();
            DataSet STemp1 = new DataSet();
            string FKRS = "", FLD = "", FPOS = "", FART = "", FVAR = "", FFARBE = "", FFINISH = "";
            bool IsHasAB55 = false, IsHasAB45 = false, IsHasAB05 = false;
            DataRow dwQueryTB = INTEXDBC.Convert2ICC_QUERYTB(null, app.company, false);
            DataTable tb = dwQueryTB.Table.Clone();
            DataRow dwData = tb.NewRow();
            dwData["EXART"] = _EXART;
            INTEXDBC.DeleteICC_QUERYTB(dwData, 2);
            DataRow dwWDECK = null;
            //获取后整理生产单
            dwData.ItemArray = dwQueryTB.ItemArray;
            dwData["VKPMANDANT"] = app.Mandant;
            dwData["VKPAUFKRS"] = _krs;
            dwData["VKPAUFLFD"] = _lfd;
            dwData["VKPAUFPOS"] = _pos;
            dwData["C2"] = "NO";
            dwData["EXART"] = _EXART;
            INTEXDBC.InsertICC_QUERYTB(dwData, 1);
            dwData["C1"] = "F";
            SResult = INTEXDBC.QueryWDECK(dwData, 22);

            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                dwData.ItemArray = dwQueryTB.ItemArray;
                dwWDECK = row;
                FKRS = dwWDECK["ABPAUFKRS"].ToString();
                FLD = dwWDECK["ABPAUFLFD"].ToString();
                FPOS = dwWDECK["ABPAUFPOS"].ToString();
                FART = dwWDECK["ABPART"].ToString();
                FVAR = dwWDECK["ABPVAR"].ToString();
                FFARBE = dwWDECK["ABPFARBE"].ToString();
                FFINISH = dwWDECK["ABPBESCHICH"].ToString();
                dwData["ABPMANDANT"] = app.Mandant;
                dwData["ABPAUFKRS"] = FKRS;
                dwData["ABPAUFLFD"] = FLD;
                dwData["ABPAUFPOS"] = FPOS;
                dwData["C2"] = "NO";
                dwData["EXART"] = _EXART;

                if (FLD == "000000")
                    continue;
                IsHasAB05 = true;
                INTEXDBC.InsertICC_QUERYTB(dwData, 1);//记录AB05
                dwData["C1"] = "R";
                STemp1 = INTEXDBC.QueryWDECK(dwData, 22);
                STemp.Merge(STemp1);
            }
            if (dwWDECK == null)
                return SReturn;
            #region===通过品种========

            dwData["ABPMANDANT"] = app.Mandant;
            dwData["ABPART"] = FART;
            dwData["ABPVAR"] = FVAR;
            dwData["ABPFARBE"] = FFARBE;
            dwData["ABPBESCHICH"] = FFINISH;
            STemp = INTEXDBC.QueryABPOS(dwData, 26);
            foreach (DataRow row in STemp.Tables[0].Rows)
            {
                dwData.ItemArray = dwQueryTB.ItemArray;
                dwData["ABPMANDANT"] = row["ABPMANDANT"];
                dwData["ABPAUFKRS"] = row["ABPAUFKRS"];
                dwData["ABPAUFLFD"] = row["ABPAUFLFD"];
                dwData["ABPAUFPOS"] = row["ABPAUFPOS"];
                dwData["C2"] = "NO";
                dwData["EXART"] = _EXART;
                INTEXDBC.InsertICC_QUERYTB(dwData, 1);//记录AB50  
            }
            SResult.Clear();
            SResult.Merge(STemp);
            STemp.Clear();
            dwData["ABPMANDANT"] = app.Mandant;
            dwData["ABPART"] = _EXART.Substring(0, 7);
            dwData["ABPFARBE"] = "-" + Convert.ToInt16(_pos);
            STemp = INTEXDBC.QueryABPOS(dwData, 28);
            foreach (DataRow row in STemp.Tables[0].Rows)
            {
                dwData.ItemArray = dwQueryTB.ItemArray;
                dwData["ABPMANDANT"] = row["ABPMANDANT"];
                dwData["ABPAUFKRS"] = row["ABPAUFKRS"];
                dwData["ABPAUFLFD"] = row["ABPAUFLFD"];
                dwData["ABPAUFPOS"] = row["ABPAUFPOS"];
                dwData["C2"] = "NO";
                dwData["EXART"] = _EXART;
                INTEXDBC.InsertICC_QUERYTB(dwData, 1);//记录AB50  
            }
            #endregion
            SResult.Merge(STemp);
            STemp.Clear();
            #region====通过可能的订单网络=========
            if (!IsHasAB05)
                return SReturn;
            dwData["ABPMANDANT"] = app.Mandant;
            dwData["ABPAUFKRS"] = FKRS;
            dwData["ABPAUFLFD"] = FLD;
            dwData["ABPAUFPOS"] = FPOS;
            dwData["C2"] = "NO";
            dwData["EXART"] = _EXART;
            INTEXDBC.InsertICC_QUERYTB(dwData, 1);//记录AB55
            STemp1 = INTEXDBC.QueryBAAUF(dwData, 6);
            STemp.Merge(STemp1);
            STemp1 = INTEXDBC.QueryGAAUF(dwData, 19);
            STemp.Merge(STemp1);
            IsHasAB55 = true;

            if (IsHasAB55)
            {
                SResult.Clear();
                SResult.Merge(STemp);
                STemp.Clear();
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    dwData.ItemArray = dwQueryTB.ItemArray;
                    dwData["ABPMANDANT"] = dwData["ABPMANDANT"];
                    dwData["ABPAUFKRS"] = dwData["ABPAUFKRS"];
                    dwData["ABPAUFLFD"] = dwData["ABPAUFLFD"];
                    dwData["ABPAUFPOS"] = dwData["ABPAUFPOS"];
                    dwData["C2"] = "NO";
                    dwData["EXART"] = _EXART;
                    INTEXDBC.InsertICC_QUERYTB(dwData, 1);//记录AB45/AB50 
                    STemp1 = INTEXDBC.QueryGAAUF(dwData, 20);
                    STemp.Merge(STemp1);
                    IsHasAB45 = true;
                }
            }
            if (IsHasAB45)
            {
                SResult.Clear();
                SResult.Merge(STemp);
                STemp.Clear();
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    dwData.ItemArray = dwQueryTB.ItemArray;
                    dwData["ABPMANDANT"] = dwData["ABPMANDANT"];
                    dwData["ABPAUFKRS"] = dwData["ABPAUFKRS"];
                    dwData["ABPAUFLFD"] = dwData["ABPAUFLFD"];
                    dwData["ABPAUFPOS"] = dwData["ABPAUFPOS"];
                    dwData["C2"] = "NO";
                    dwData["EXART"] = _EXART;
                    INTEXDBC.InsertICC_QUERYTB(dwData, 1);//记录AB50  
                }
            }
            #endregion

            return SReturn;
        }
        public override void CallbackImpl(IAsyncResult run, DataSet _SResult)
        {
            base.CallbackImpl(run, _SResult);
            AsyncRunHandle handle = run.AsyncState as AsyncRunHandle;
            switch (handle.Method.Name)
            {
                case "StartPlan":
                    Task.Factory.StartNew(() => MergeData(null), new CancellationTokenSource().Token, TaskCreationOptions.None, _sync).Wait();
                    break;
                case "YGRPLANDB":
                case "JLTPLANDB": 
                    break;
                default:
                    break;
            }
        }

      }
}